home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 90 / CD Actual 90.iso / Software3D / K-3D / k3d-0.4.2.1 / shaders / k3d_eyeball.sl < prev    next >
Encoding:
Text File  |  2004-07-23  |  5.3 KB  |  139 lines

  1. /*
  2.  * eyeball.sl -- RenderMan compatible shader for an eyeball.
  3.  *
  4.  * DESCRIPTION:
  5.  *   Makes a plastic-like surface which looks like an eyeball.  It's meant
  6.  *   for use on a sphere.  The center of the pupil is at the "north pole",
  7.  *   i.e. where the t parameter is 1.  The colors of the pupil, iris, white
  8.  *   part (eyeball), and blood vessels can be set individually.  Fractal
  9.  *   functions are used for the veining and the iris mottling.
  10.  * 
  11.  * PARAMETERS:
  12.  *   Ka, Kd, Ks, roughness, specularcolor - work just like the plastic shader
  13.  *   iriscolor - color of the iris
  14.  *   eyeballcolor - color of the white part of the eyeball
  15.  *   bloodcolor - color of the blood vessels
  16.  *   pupilcolor - color of the pupil (opening)
  17.  *   pupilsize - size of pupil (in "t" space)
  18.  *   irissize - size of iris (in "t" space), must be larger than pupilsize
  19.  *   bloodshot - controls how bloodshot the eye is (0=no blood, 1=very ugly)
  20.  *   veinfreq, veinlevel - control the formation of the blood vessels
  21.  *   index - set between 0 and 1, lets you use this shader to generate
  22.  *           non-identical eyeballs.
  23.  *
  24.  * ANTIALIASING: basic antialiasing of the boundaries between tissue types
  25.  *               is performed.
  26.  *
  27.  * AUTHOR: written by Larry Gritz
  28.  *
  29.  * HISTORY:
  30.  *      Nov 1991 - crude written of "eye" by lg for Herman's eyes for
  31.  *                 "Graphic Violence".  Original version hard coded in C.
  32.  *      Dec 1993 - "eye" modified by lg to clean up a bit.
  33.  *      10 Jan 1994 - recoded by lg in correct shading language.
  34.  *      28 Jun 94 (lg) - revamped to add veins and iris mottling, renamed
  35.  *                       "eyeball"
  36.  *       7 Jan 95 (wave) - changed name to LGEyeBall for namespace reasons...
  37.  *       8 Jan 95 (wave) - changed Ciris line to fix bug Larry figured out and changed defaults
  38.  *       27 Feb 95 (wave) - changed PO line to fix bug Larry figured out to actually *use* index
  39.  *
  40.  * last modified  8 Jan 95 by Michael B. Johnson (wave)
  41.  */
  42.  
  43.  
  44.  
  45. surface
  46. k3d_eyeball (float Ka = .75, Kd = 0.75, Ks = 0.4, roughness = 0.1;
  47.      color specularcolor = 1;
  48.      color iriscolor = color (.135289, .084323, .372417);
  49.      color irisoutercolor = color (.403882, .343944, .68276);
  50.      color irisinnercolor = color (.065142, .040605, .179311);
  51.      color eyeballcolor = color(1,1,1);
  52.      color bloodcolor = color(.8,.05,.05);
  53.      color pupilcolor = 0;
  54.      float pupilsize = 0.05, irissize = 0.12;
  55.      float bloodshot = 1.0;
  56.      float veinfreq = 8, veinlevel = 4;
  57.      float index = 0;
  58.         )
  59. {
  60. #define snoise(P) (2*noise(P)-1)
  61. #define MINFILTERWIDTH 1.0e-7
  62.   color Ct;
  63.   point Nf;
  64.   point PP, PO;
  65.   float i, turb, newturb, freq, f2;
  66.   float displayed, newdisp;
  67.   color Cball, Ciris;
  68.   float irisstat, pupilstat;
  69.   float bloody, tt;
  70.   float ks, rough;
  71.   float twidth, cutoff;
  72.  
  73.   /* Calculate an appropriate filter width for antialiasing */
  74.   twidth = max (abs(Du(t)*du) + abs(Dv(t)*dv), MINFILTERWIDTH);
  75.   PO = transform ("object", P) + index;
  76.  
  77.   /* Figure out where we are in the eyeball.  Use the following variables:
  78.    * irisstat: 0 inside the iris/white boundary, 1 outside
  79.    * pupilstat: 0 inside the pupil/iris boundary, 1 outside
  80.    * bloody: how potentially bloody it is (fade as we get away from iris)
  81.    */
  82.   tt = 1-t;
  83.   irisstat = smoothstep (irissize, irissize+twidth, tt);
  84.   pupilstat = smoothstep (pupilsize, pupilsize+twidth, tt);
  85.   bloody = bloodshot * (smoothstep (-irissize, 2.5*irissize, tt));
  86.  
  87.   /* If we're somewhere in the white part and it's potentially bloody,
  88.    * then calculate the veining pattern.  Otherwise, just use the color
  89.    * of the whites.  The veining pattern is essentially summed zero sets
  90.    * of turbulence functions.  Some stretching is done to get it to look
  91.    * just right.
  92.    */
  93.   if (irisstat * bloody > 0.001) {
  94.       turb = bloody;  freq = veinfreq;
  95.       displayed = 0;
  96.       for (i = 1;  (i <= veinlevel) && (turb > 0.1);  i += 1) {
  97.       newturb = 1 - abs (snoise(PO*freq + point(0,0,20*freq)));
  98.       newdisp = pow (smoothstep (.85, 1, newturb), 10);
  99.       displayed += (1-displayed) * newdisp * smoothstep (.1, .85, turb * turb);
  100.       turb *= newturb;
  101.       freq *= 2;
  102.         }
  103.       Cball = mix (eyeballcolor, bloodcolor, smoothstep(0,.75,displayed));
  104.     }
  105.   else Cball = eyeballcolor;
  106.  
  107.   Ciris = mix (iriscolor, irisoutercolor, smoothstep (irissize*.8, irissize, tt));
  108.   /* If we're somewhere in the iris, calculate the iris pattern, which is
  109.    * just a stretched turbulence function.
  110.    */
  111.   if (irisstat < 0.9999 && pupilstat > 0.0001) {
  112.       turb = 0;  freq = 1;  f2 = 30;
  113.       for (i = 1;  i <= 4;  i += 1) {
  114.       turb += snoise (PO*f2 + point(0,0,20*f2)) / freq;
  115.       freq *= 2;  f2 *= 2;
  116.         }
  117.       Ciris *= (1-clamp(turb/2,0,1));
  118.     }
  119.  
  120.   /* OK, now calculate a surface texture color (Ct) based on where we are
  121.    * and what patterns we calculated.
  122.    */
  123.   Ct = mix (Ciris, Cball, irisstat);
  124.   Ct = mix (pupilcolor, Ct, pupilstat);
  125.  
  126.   /* Make the eye a little glossier on the iris and pupil */
  127.   ks = Ks * (1+2*(1-irisstat));
  128.   rough = roughness * (1-.75*(1-irisstat));
  129.  
  130.   /* Now shade like plastic, but using our calculated surface color and
  131.    * our modified values for roughness and Ks.
  132.    */
  133.   Oi = Os;
  134.   Nf = faceforward (normalize(N),I);
  135.   Ci = Os * ( Ct * (Ka*ambient() + Kd*diffuse(Nf)) +
  136.           specularcolor * ks*specular(Nf,-normalize(I),rough));
  137. }
  138.  
  139.